﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using MTBrainStorm.Framework;
using MTBrainStorm.Framework.Messaging;

namespace MTBrainStorm.ViewModels
{
    class TextElementViewModel : MultiTouchElementViewModel, IDocumentElementViewModel
    {
        public VirtualKeyboardViewModel AttachedKeyboard { get; set; }

        private string _text;
        public string Text
        {
            get { return _text; }
            set 
            { 
                _text = value;
                RaisePropertyChanged("Text");
            }
        }

        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                RaisePropertyChanged("IsActive");
            }
        }

        public TextElementViewModel()
        {
            AttachedKeyboard = new VirtualKeyboardViewModel();
            AttachedKeyboard.ScaleX = .75;
            AttachedKeyboard.ScaleY = .75;
            AttachedKeyboard.PropertyChanged += TextChanged;
        }

        void TextChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Text")
                Text = AttachedKeyboard.Text;
        }

        private RelayCommand _toggleKeyboardCommand;
        public ICommand ToggleKeyboardCommand
        {
            get { return _toggleKeyboardCommand ?? (_toggleKeyboardCommand = new RelayCommand(ExecuteToggleKeyboardCommand)); }
        }

        private void ExecuteToggleKeyboardCommand()
        {
            IsActive = !IsActive;
            AttachedKeyboard.Text = Text;
            AttachedKeyboard.Position = Position;
            AttachedKeyboard.Rotation = Rotation;
            if(IsActive)
            {
                var msg = new KeyValuePair<string, TextElementViewModel>("show", this);
                Messenger.Default.Send(msg);
            }
            else
            {
                var msg = new KeyValuePair<string, TextElementViewModel>("hide", this);
                Messenger.Default.Send(msg);
            }
            
        }
    }
}
